package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.HktsWasteGas;
import com.hkts.naturalgas.base.entity.Ozone;
import com.hkts.naturalgas.base.entity.RoastingDesulphurizing;
import com.hkts.naturalgas.base.mapper.OzoneMapper;
import com.hkts.naturalgas.base.service.IOzoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class OzoneServiceImpl extends ServiceImpl<OzoneMapper, Ozone> implements IOzoneService {


    @Autowired
    private OzoneMapper ozoneMapper;

    @Override
    public IPage<Ozone> selectPage(Page<Ozone> page, Ozone ozone){
            QueryWrapper<Ozone> query=Wrappers.query();
            QueryWrapper<Ozone> entityWrapper=getEntityWrapper(query, ozone);
            return ozoneMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<Ozone> selectList(Ozone ozone){
            QueryWrapper<Ozone> query=Wrappers.query();
            QueryWrapper<Ozone> entityWrapper=getEntityWrapper(query, ozone);
            return ozoneMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<Ozone> getEntityWrapper(QueryWrapper<Ozone> entityWrapper,Ozone ozone){
            //条件拼接
            return entityWrapper;
    }
    @Override
    public IPage<Ozone> selectPageByTime(Page<Ozone> page, String startDate, String endDate){
        QueryWrapper<Ozone> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(Ozone::getTime);
        IPage<Ozone> hktsTailGasPage = ozoneMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<Ozone> getBrokenLineGraph(Page<Ozone> page, String startDate, String endDate){
        QueryWrapper<Ozone> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(Ozone::getTime);
        IPage calcinationDesulfurizationDenitrations = ozoneMapper.selectPage(page,query);
        List<Ozone> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(Ozone vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("1#臭氧流量(nm3/h)",vo.getBsjhCy1());
        data.put("1#臭氧浓度(mg/l)",vo.getBsjhCy1c());
        data.put("2#臭氧浓度(mg/l)",vo.getBsjhCy2c());
        data.put("2#臭氧流量(nm3/h)",vo.getBsjhCy2f());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void deleteAll() {
        ozoneMapper.deleteAll();
    }

    @Override
    public List<Ozone> selectListBytime() {

        QueryWrapper<Ozone> query=Wrappers.query();
        query.ge(Ozone.TIME,"2020-08-25 00:00:00");
        query.le(Ozone.TIME,"2021-05-19 00:30:00");
        List<Ozone> ozones = ozoneMapper.selectList(query);
        return ozones;
    }
}
